<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
	Name	: simpanan class controller
	Author	: Iswan Putera S.Kom
	Date	: 18/07/2013
	Version : 1.0.0
*/

class Simpanan extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library('zetro_auth');
		$this->load->model('main_model');
		$this->zetro_auth->check_login();
		$this->load->library('zetro_func');
		$this->load->library('zetro_terbilang');//force_ssl();
	}
	
	function View($data='',$filename="",$path="simpanpinjam/")
	{
		$dd['notifikasi']='1';
		$dd['title']="Ada Pengajuan pinjaman baru\n Silahkan klik untuk proses pengajuan ini";
		$this->load->view('admin/header',$dd);
		$this->load->view($path.$filename,$data);
		$this->load->view('admin/footer');
	}
	/**
		call view for input setoran
	*/
	function setor()
	{
		$mdl=(base64_decode($this->session->userdata('menus'))=='Simpan Pinjam')?array("id_simpanan < "=>"4"):'';
		$data['tab_header']=$this->zetro_form->generate_tab_header('Input Setoran');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_form->section('setoran');
		$this->zetro_auth->menus('setoran_tunai');
		$this->zetro_form->get_select_opt();
		$this->zetro_form->tp_ahead($this->get_akun(/*array('igrup' =>'1')*/));
		$this->zetro_form->addButtons(true);
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['form']=$this->zetro_form->frm_std();
		$data['simp']=$this->zetro_form->generate_dropdown_content('simpanan_jenis','id_simpanan','nm_simpanan',$mdl);
		//list non tunai
		//generate list dropdown bulan tahun -2 +1
		$tahun="<option value='".(date('Y')-2)."'>".(date('Y')-2)."</option>";
		$tahun.="<option value='".(date('Y')-1)."'>".(date('Y')-1)."</option>";
		$tahun.="<option value='".(date('Y'))."' selected>".(date('Y'))."</option>";
		$tahun.="<option value='".(date('Y')+1)."'>".(date('Y')+1)."</option>";
		$data['blne']=dropdownBln(date('m'));
		$data['thn']=$tahun;
			$this->zetro_auth->menus('setoran_non_tunai');
		
			$this->zetro_form->buttons(false,'Process');
			$this->zetro_form->section('frmnontunai');
			$data['frm2']=$this->zetro_form->frmInline('frm2',false,($this->zetro_auth->cek_oto('c')!='')?true:false,true);
			$data['lok']=$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
			//create list
			$content='';
			$this->zetro_form->section('nontunai');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->headernontunai();//$this->zetro_form->list_std();
			$data['modal2']=$this->zetro_form->PrintPreview('Print Kwitansi');
		$this->load->view('admin/header');
		$this->load->view('simpanpinjam/setoran',$data);
		$this->load->view('admin/footer');
	}
	function headernontunai()
	{
		$content=tabel('table table-bordered table-hover').
				 tr('alert alert-info',true,'center').
				 td('No.','4%',true,'','center',"rowspan='2'").
				 td('No.Anggota','8%',true,'','center',"rowspan='2'").
				 td('Nama Anggota','13%',true,'','center',"rowspan='2'").
				 td('Wajib','8%',true,'','center',"rowspan='2'").
				 td('Sukarela','8%',true,'','center',"rowspan='2'").
				 td('Pinj.Barang.','8%',true,'','center',"rowspan='2'").	
				 td('Pinj.Uang','15%',true,'','center',"colspan='2'").
				 td('Pinj.Non Kredit','15%',true,'','center',"colspan='2'").
				 td('','4%',true,'','center',"rowspan='2'").
				_tr().
				 tr('alert alert-info',false,'center').
				 td('Pokok','5%',true,'','center').
				 td('Bunga','5%',true,'','center').
				 td('Pokok','5%',true,'','center').
				 td('Bunga','5%',true,'','center').
				_tr(true).
				
				tContent(true).
				_tabel();
		return $content;
	}
	function setorpinjaman()
	{
		$mdl=(base64_decode($this->session->userdata('menus'))=='Simpan Pinjam')?array("id_simpanan >= "=>"4"):'';
		$data['tab_header']=$this->zetro_form->generate_tab_header('Angsuran Pinjaman');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_form->section('setoranpinjaman');
		$this->zetro_auth->menus('angsuran_pinjaman');
		$this->zetro_form->addButtons(false);
		$this->zetro_form->get_select_opt();
		$this->zetro_form->tp_ahead($this->get_akun(/*array('igrup' =>'1')*/));
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['form']=$this->zetro_form->frm_std();
		$data['simp']=$this->zetro_form->generate_dropdown_content('simpanan_jenis','id_simpanan','nm_simpanan',$mdl);
		//list non tunai
		//generate list dropdown bulan tahun -2 +1
		$tahun="<option value='".(date('Y')-2)."'>".(date('Y')-2)."</option>";
		$tahun.="<option value='".(date('Y')-1)."'>".(date('Y')-1)."</option>";
		$tahun.="<option value='".(date('Y'))."' selected>".(date('Y'))."</option>";
		$tahun.="<option value='".(date('Y')+1)."'>".(date('Y')+1)."</option>";
		$data['blne']=dropdownBln(date('m'));
		$data['thn']=$tahun;
		$this->load->view('admin/header');
		$this->load->view('simpanpinjam/setoran_pinjaman',$data);
		$this->load->view('admin/footer');
	}
	/**
		collect data akun to array for typeahead purpose
	*/
	function get_akun($where='')
	{
		$data=array();$akuns="";
		$this->main_model->where($where);
		$this->main_model->table_name('v_akun_display');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		//$akuns='[';
		foreach($data as $r)
		{
			$akuns.="&quot;".$r->kodeakun."&quot;,";
		}
		//$akuns.=']';
		return substr($akuns,0,-1);
	}
	function get_kdcc($where='')
	{
		$data=array();$akuns="";
		$this->main_model->where($where);
		$this->main_model->table_name('akun_cc_head');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		//$akuns='[';
		foreach($data as $r)
		{
			$akuns.="&quot;".$r->nm_cost."&quot;,";
		}
		//$akuns.=']';
		return substr($akuns,0,-1);
	}
	/**
		collect data akun to array for typeahead purpose
		@output		: JSON
	*/
	function kontraakun()
	{
		/**
			data akun
		*/
		$data=array();$akuns="";$nama="";
		$kd=$_POST['st'];
		//$this->main_model->where(array('app_stat'=>"R"));
		$this->main_model->table_name('v_akun_display');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$akuns[]=$r->kodeakun;
		}
		echo json_encode($akuns);
	}
	function getakunreg()
	{
		/**
			register member
		*/
		$data=array();$akuns="";$nama="";
		$kd=$_POST['st'];
		$this->main_model->where(array('app_stat'=>"R"));
		$this->main_model->table_name('member');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$akuns[]=$r->id_reg;
			$nama[]=$r->nm_member;
		}
		echo ($kd=="R")?json_encode($akuns):json_encode($nama);
	}
	function getakunmember()
	{
		/** 
			Angggota
		*/
		$data=array();$akuns="";$nama="";
		$kd=$_POST['st'];
		$keluar=empty($_POST['stat'])?'':$_POST['stat'];
		($keluar!='')?$this->main_model->where(array('status_member'=>$keluar)):'';
		$this->main_model->where(array('app_stat'=>"Y"));
		$this->main_model->orderby('nm_member');
		$this->main_model->table_name('anggota_v');
		$this->main_model->set_methods('select_ordered');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$akuns[]=$r->id_reg;
			$nama[]=$r->nm_member;
		}
		echo ($kd=="R")?json_encode($akuns):json_encode($nama);
	}
	function getakundropdown()
	{
		echo $this->zetro_form->generate_dropdown_content('akun','ckode','cnama',array('cid_parent'=>'113'));	
	}
	function getkdbarang()
	{
		/**
		* kode barang
		*/
		$data=array();$akuns="";$nama="";
		$kd=$_POST['st'];
		$this->main_model->table_name('inv_barang');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$akuns[]=$r->nm_barang." | ".$r->id.".".$r->id_unit;
		}
		echo json_encode($akuns);
	}
	/**
		detail member data
		@param		: id_reg
		@param		: nm_reg
	*/
	function get_akun_detail()
	{
		$id=empty($_POST['id'])?'':$_POST['id'];
		$this->main_model->where(array('id_reg'=>$id));
		$this->main_model->where_or(array('nm_member'=>$id));
		$this->main_model->table_name('member');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		echo empty($data)?$this->get_anggota_detail($id):json_encode($data[0]);	
	}
	
	function get_anggota_detail($id)
	{
		$this->main_model->where(array('id_reg'=>$id));
		$this->main_model->where_or(array('nm_member'=>$id));
		$this->main_model->table_name('anggota');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		return empty($data)?json_encode("{'id_reg':''}"):json_encode($data[0]);	
	}
	/**
		simpan setoran tunai ke transaksi_member
		@param	:id_member
		@param	:kd_akun
		Triger akan dijalankan
			-update saldo simpanan/pinjaman
			-masukan inputan ke dalam jurnal
	*/
	function set_setoran()
	{
		$data=array();
		$tgl=explode("/",$_POST['tgl_trans']);
		$j_trans=$_POST['tp_setoran'];
		$data['id_trans']   =empty($_POST['id_trans'])?'0':$_POST['id_trans'];
		$data['tgl_trans']  =empty($_POST['tgl_trans'])?date('Ymd'):tgltoSql($_POST['tgl_trans']);
		$data['jenis_trans']=empty($_POST['tp_setoran'])?'':$_POST['tp_setoran'];
		$data['bln']		=$tgl[1];
		$data['tahun']	  =$tgl[2];
		$data['id_unit']	='1';
		$data['id_member']  =($j_trans!='Registrasi')?$_POST['no_member']:$_POST['no_member_r'];
		$data['id_simpanan']=empty($_POST['tp_simpanan'])?'':$_POST['tp_simpanan'];
		$data['kredit']     =empty($_POST['jml_simpanan'])?'0':str_replace(",","",$_POST['jml_simpanan']);
		$data['debet']	  =0;
		$data['id_akun']    =empty($_POST['kd_akun'])?'':$_POST['kd_akun'];
		$data['id_akun_kontra']=empty($_POST['kd_akun2'])?'':$_POST['kd_akun2'];
		$data['ket_trans']  =empty($_POST['ket_trans'])?'':$_POST['ket_trans'];
		$data['stat_trans'] =empty($_POST['stat_trans'])?'N':$_POST['stat_trans'];
		$data['created_by'] =$this->session->userdata("userid");
		$this->main_model->prepared_data('transaksi_member',$data);
		echo success($this->main_model->set_data()).":".$this->main_model->rdb("transaksi_member","id_trans","order by id_trans desc limit 1");
		
	}
	function print_kwitansi()
	{
		$data=array();
		$data['coy']	=$this->zetro_form->infoCo('Nama');
		$data['add']	=$this->zetro_form->infoCo('Address');
		$data['kota']   =$this->zetro_form->infoCo('City');
		$data['keua']   =$this->zetro_form->infoCo('AccHead');
		$id=$_POST['id'];
		$this->main_model->where(array('id_trans'=>$id));
		$this->main_model->table_name('transaksi_member_v');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data['lst']=$this->main_model->set_data();
		//print_r($data);
		$this->load->library('zetro_dompdf');
		$html = $this->load->view('simpanpinjam/setoran_kwt', $data, true);
		$this->zetro_dompdf->createPDF($html, $this->session->userdata('userid').'_kwitansi');
		echo base_url().'application/logs/'.$this->session->userdata('userid').'_kwitansi.pdf';
	}
	/**
		Update status registrator menjadi member aktif setelah membayar biaya registrasi
		set setoran berhasil
		@param	: id_reg
		change to trigger method
	*/
	
	function update_register()
	{
		$id_reg=$_POST['no_member_r'];
		$this->main_model->table_name('member_reg');
		$this->main_model->set_methods('update');
		$this->main_model->get_content(array('app_stat'=>'Y'));
		$this->main_model->where(array('id_reg'=>$id_reg));
		$this->main_model->set_data();	
	}
	/** 
	  member list untuk proses pembayaran non tunai atau transfer
	*/
	function get_list_member()
	{
		$content="";$n=0;
		$id_lok=empty($_POST['id_cabang'])?'':$_POST['id_cabang'];
		$thn=$_POST['tahun'];
		$bln=$_POST['bln'];
		$lst_usr=array();
			$ordby=empty($_POST['ordby'])?'nm_member':$_POST['ordby'];
			$this->main_model->table_name('v_member_sp');
			$this->main_model->query();
			(empty($_POST['id_cabang'])||$_POST['id_cabang']=='')?'':
			$this->main_model->where(array('id_lokasi'=>$id_lok,'c_bayar'=>'2'));
			$this->main_model->where(array('status_member <>'=>'N'));
			(empty($_POST['cari']))?'':
			$this->main_model->where_like('nm_member',$_POST['cari'],'both');
			$this->main_model->group_by('id_reg');
			$this->main_model->orderby($ordby);
			$this->main_model->set_methods('select_ordered');
			$lst_usr=$this->main_model->set_data();
			$this->zetro_auth->menus('setoran_non_tunai');
			foreach($lst_usr as $r)
			{
				 
				$n++;
				$oto=$this->zetro_auth->cek_oto('c');$posting='';$ck="";$check="";
				$check=($this->main_model->get_db('transaksi_member_tmp',array('id_member'=>$r->id_reg,'bln'=>$bln,'tahun'=>$thn),'id_member'));
				$posting=$this->main_model->get_db('transaksi_member_tmp',array('id_member'=>$r->id_reg,'bln'=>$bln,'tahun'=>$thn),'posting');
				$ck=($check==''|| $check=='0')?'':"checked='checked'";
				$icon=($ck=='')?"<i class='cus-picture-empty'></i>":"<i class='cus-tick'></i>";
				$centang=($posting!='N')?$icon:"<input type='checkbox' ".$ck." id='".str_replace('-','',$r->id_reg)."' name='".str_replace('-','',$r->id_reg)."' onClick=\"_aksi(this,'".$r->id_reg."');\"/>";
				$content.=tr().td($n,false,'','','center').
						  td($r->id_reg,false,'','','center').
						  td($r->nm_member).
						  td(number_format($this->_get_default_simpanan($r->id_reg,'2'),0),false,'','','right').
						  td(number_format($this->_get_default_simpanan($r->id_reg,'3'),0),false,'','','right').
						  td(number_format($this->_get_default_simpanan($r->id_reg,'4'),0),false,'','','right').
						  td(number_format($this->_get_default_simpanan($r->id_reg,'5'),0),false,'','','right').
						  td(number_format($this->_get_default_simpanan($r->id_reg,'5','bunga_pinjaman'),0),false,'','','right').
						  td(number_format($this->_get_default_simpanan($r->id_reg,'6'),0),false,'','','right').
						  td(number_format($this->_get_default_simpanan($r->id_reg,'6','bunga_pinjaman'),0),false,'','','right').
						  td(($oto=='')?$icon:$centang,false,'','','center').
						  _tr();
						  
			}
		$content.=$this->zetro_form->DataNotFound($lst_usr,10);
		echo $content;
	}
	function _get_default_simpanan($id_member,$id_simpanan,$fld='val_simpanan')
	{
		/**
		get default simpanan member
		*/
		$val=0;$lst_usr=array();
		$this->main_model->table_name('member_set_simpanan');
		$this->main_model->query();
		$this->main_model->where(array('id_member'=>$id_member,'id_simpanan'=> $id_simpanan));
			$this->main_model->set_methods('select');
			$lst_usr=$this->main_model->set_data();
			foreach($lst_usr as $rr)
			{
				$val=$rr->$fld;
			}
		return $val;
	}
	/**
		Simpan data pembayaran non tunai ke table sementara
	*/
	function set_setoran_nt()
	{
		$data=array();
		$id=$_POST['id_member'];
		$bln=$_POST['bln'];
		$thn=$_POST['tahun'];
		$nm=strtoupper($this->main_model->get_db('anggota',array("id_reg"=>$_POST['id_member']),'nm_member'));
		for($i=1;$i<=5;$i++)
		{
			/*switch($i)
			{
				case '1':
				
				$ids='2';*/
					$jp=($i<=3)?"Simpanan":"Pinjaman";	
					$ida=$this->main_model->get_db('simpanan_jenis',array("id_simpanan"=>($i+1)),'cost_cc');
					$data=array();$datax=array();$n=0;
					$this->main_model->table_name('akun_cc_view');
					$this->main_model->set_methods('select');
					$this->main_model->query();
					$this->main_model->where(array('parent_cc'=>$ida));
					$datax=$this->main_model->set_data();
					$kontra=0;
					foreach($datax as $r)
					{
						$n++;
						$jml=0;$jml_bunga=0;
						$ket=$r->nm_cc.'-'.$nm.' - '.nBulan($bln,true).' '.$thn;
						$jml=$this->_get_default_simpanan($id,($i+1));
						$jml_bunga=$this->_get_default_simpanan($id,($i+1),'bunga_pinjaman');
						$jml=(substr($r->id_akun,0,5)=='41010')?$jml_bunga:$jml;
						$kontra =($n>2)?($kontra+$jml):$jml;
						$cabang=$this->main_model->get_db('anggota',array('id_reg'=>$_POST['id_member']),'id_lokasi');
						$data['tgl_trans']=empty($_POST['tgl_skr'])?date('Ymd'):tglToSql($_POST['tgl_skr']);
						$data['jenis_trans']=$jp;
						$data['id_member']=$id;
						$data['id_simpanan']=($i+1);
						$data['bln']		=$bln;
						$data['tahun']	  =$thn;
						$data['id_cabang'] =empty($_POST['id_cabang'])?$cabang:$_POST['id_cabang'];
						$data['debet']	 =($r->id_calc==0)?$jml:0;
						$data['kredit']	=($r->id_calc==1)?$kontra:0;
						$data['id_akun']   =($r->id_calc==0)?$r->kd_akun:'';
						$data['id_akun_kontra']=($r->id_calc==1)?$r->kd_akun_kontra:'';
						$data['ket_trans'] =$ket;
						$data['id_unit']   ='1';
						$data['id_cc']	=$r->id_cc;
						$data['created_by']=$this->session->userdata('userid');
						$this->main_model->prepared_data('transaksi_member_tmp',$data);
						($jml <= 0)?'':$this->main_model->set_data();
					}
			
/*			$kredit=0;
				break;
				case '2':
				$jp='Simpanan';
				$ids='3';
				$ida=$this->main_model->get_db('simpanan_jenis',array("id_simpanan"=>'3'),'id_akun');
				$ket='Setoran Simpanan Sukarela -'.$nm.' - '.nBulan($bln,true).' '.$thn;
				$jml=$this->_get_default_simpanan($id,'3');
				$jml_k=0;
				$jml_bunga=0;
				break;
				case '3':
				$jp='Simpanan';
				$ids='4';
				$ida=$this->main_model->get_db('simpanan_jenis',array("id_simpanan"=>'4'),'id_akun');
				$ket='Setoran Angsuran Pinjaman Barang -'.$nm.' - '.nBulan($bln,true).' '.$thn;
				$jml_k=$this->_get_default_simpanan($id,'4');
				$jml=0;
				$jml_bunga=0;
				break;
				case '4':
				$jp='Simpanan';
				$ids='5';
					$ida=$this->main_model->get_db('simpanan_jenis',array("id_simpanan"=>'5'),'id_akun');
					$ket='Setoran Angsuran Pinjaman Tunai-'.$nm.' - '.nBulan($bln,true).' '.$thn;
					$jml_k=$this->_get_default_simpanan($id,'5');
					
					$jml_bunga=$this->_get_default_simpanan($id,'5','bunga_pinjaman');
					if($jml_bunga >0)
					{
						$cabang=$this->main_model->get_db('anggota',array('id_reg'=>$_POST['id_member']),'id_lokasi');
						$data['tgl_trans']=empty($_POST['tgl_skr'])?date('Ymd'):tglToSql($_POST['tgl_skr']);
						$data['jenis_trans']=$jp;
						$data['id_member']=$_POST['id_member'];
						$data['id_simpanan']=$ids;
						$data['bln']		=$bln;
						$data['tahun']	  =$thn;
						$data['id_cabang'] =empty($_POST['id_cabang'])?$cabang:$_POST['id_cabang'];
						$data['kredit']	=$jml;
						$data['debet']	 =$jml_k;
						$data['id_akun']   =$ida;
						$data['id_akun_kontra']='11301';
						$data['ket_trans'] =$ket;
						$data['id_unit']   ='1';
						$data['created_by']=$this->session->userdata('userid');
						$this->main_model->prepared_data('transaksi_member_tmp',$data);
						$this->main_model->set_data();
					}
				$jml=0;
				break;
				case '5':
				$jp='Simpanan';
					$ids='6';
					$ida=$this->main_model->get_db('simpanan_jenis',array("id_simpanan"=>'6'),'id_akun');
					$ket='Setoran Angsuran Pinjaman Barang Non Kredit -'.$nm.' - '.nBulan($bln,true).' '.$thn;
					$jml_k=$this->_get_default_simpanan($id,'6');
					$jml_k+=$this->_get_default_simpanan($id,'6','bunga_pinjaman');
					$jml=0;
				break;
			}
				$cabang=$this->main_model->get_db('anggota',array('id_reg'=>$_POST['id_member']),'id_lokasi');
				$data['tgl_trans']=empty($_POST['tgl_skr'])?date('Ymd'):tglToSql($_POST['tgl_skr']);
				$data['jenis_trans']=$jp;
				$data['id_member']=$_POST['id_member'];
				$data['id_simpanan']=$ids;
				$data['bln']		=$bln;
				$data['tahun']	  =$thn;
				$data['id_cabang'] =empty($_POST['id_cabang'])?$cabang:$_POST['id_cabang'];
				$data['kredit']	=$jml;
				$data['debet']	 =$jml_k;
				$data['id_akun']   =$ida;
				$data['id_akun_kontra']='11301';
				$data['ket_trans'] =$ket;
				$data['id_unit']   ='1';
				$data['created_by']=$this->session->userdata('userid');
				$this->main_model->prepared_data('transaksi_member_tmp',$data);
				echo ($jml!=0 || $jml_k!=0)?success($this->main_model->set_data()):'';*/
			
		}	
	}
	//hapus temporary data setoran non tunai
	function del_setoran_nt()
	{
		$id=$_POST['id_member'];
		$this->main_model->table_name('transaksi_member_tmp');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id_member'=>$id,'bln'=>$_POST['bln'],'tahun'=>$_POST['tahun']));
		echo success($this->main_model->set_data());	
	}
	
	//pindahkan data setoran non tunai dari tabel tmp ke tabel transaksi
	function publish_setoran_nt()
	{
		$n=0;
		$bln=$_POST['bln'];
		$thn=$_POST['tahun'];
		$lok=empty($_POST['id_cabang'])?'':$_POST['id_cabang'];
		$val=0;$lst_usr=array();
		$this->main_model->table_name('transaksi_member_tmp');
		$this->main_model->query();
		$this->main_model->where(array('bln'=>$bln,'tahun'=> $thn,'posting'=>'N'));
		empty($_POST['id_cabang'])?'':
		$this->main_model->where(array('id_cabang'=>$lok));
			$this->main_model->set_methods('select');
			$lst_usr=$this->main_model->set_data();
			//print_r($lst_usr);
			foreach($lst_usr as $r)
			{
				$n++;
				$data['tgl_trans']  =$r->tgl_trans;
				$data['jenis_trans']=$r->jenis_trans;
				$data['bln']		=$r->bln;
				$data['tahun']	  =$r->tahun;
				$data['id_unit']	=$r->id_unit;
				$data['id_member']  =$r->id_member;
				$data['id_simpanan']=$r->id_simpanan;
				$data['kredit']     =$r->kredit;
				$data['debet']	  =$r->debet;
				$data['id_akun']    =$r->id_akun.": ".$this->main_model->get_db('akun',array('ckode'=>$r->id_akun),'cnama');
				$data['id_akun_kontra']=$r->id_akun_kontra.": ".$this->main_model->get_db('akun',array('ckode'=>$r->id_akun_kontra),'cnama');;
				$data['ket_trans']  =$r->ket_trans;
				$data['created_by'] =$this->session->userdata("userid");
				$this->main_model->prepared_data('transaksi_member',$data);
				$this->main_model->set_data();
				//update status pembayaran di tmp tabel
				$this->main_model->get_content(array('posting'=>'Y'));
				$this->main_model->where(array('id_trans'=>$r->id_trans));
				$this->main_model->update_data('transaksi_member_tmp');
				
			
			}
			echo $n." Record berhasil diupdate";
	}
	/**
		Penarikan session
		call view for penarikan 
	*/
	function tarik()
	{
		$mdl=(base64_decode($this->session->userdata('menus'))=='Simpan Pinjam')?array("id_simpanan < "=>"4"):'';
		$data['tab_header']=$this->zetro_form->generate_tab_header('Input Penarikan');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_form->section('frmpenarikan');
		$this->zetro_auth->menus('penarikan_simpanan');
		$this->zetro_form->addButtons(false);
		$this->zetro_form->get_select_opt();
		$this->zetro_form->tp_ahead($this->get_akun(/*array('igrup' =>'1')*/));
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['form']=$this->zetro_form->frm_std('frm1','80%');
		//form tarik total
		$this->zetro_form->section('frmtarikkeluar');
		$this->zetro_auth->menus('penarikan_total');
		$this->zetro_form->addButtons(false);
		$this->zetro_form->get_select_opt();
		$this->zetro_form->tp_ahead($this->get_akun(/*array('igrup' =>'1')*/));
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false,'Saved');
		$data['form2']=$this->zetro_form->frm_std('frm2','80%');
		
		$this->load->view('admin/header');
		$this->load->view('simpanpinjam/penarikan',$data);
		$this->load->view('admin/footer');
	}
	
	/**
	 get saldo simpanan sukarela anggota
	 
	 */
	 function getsaldosimpanan()
	 {
		$data=0;
		$id=$_POST['id_member'];
		$fld=empty($_POST['fld'])?"saldo_sukarela":$_POST['fld'];
		$this->main_model->table_name('anggota_v_sk');
		$this->main_model->query();
		$this->main_model->where(array('id_reg'=>$id));
			$this->main_model->set_methods('select');
			$lst_usr=$this->main_model->set_data();
			foreach($lst_usr as $r)
			{
				$data=$r->$fld;
			}
		echo $data;
	 }
	 function getjmlangsuran()
	 {
		$data=array();
		$id=$_POST['id'];
		$fld=empty($_POST['tp'])?"4":$_POST['tp'];
		$this->main_model->table_name('member_set_simpanan_p');
		$this->main_model->query();
		$this->main_model->where(array('id_member'=>$id,'id_simpanan'=>$fld));
			$this->main_model->set_methods('select');
			$lst_usr=$this->main_model->set_data();
			foreach($lst_usr as $r)
			{
				$data['pokok']=$r->val_simpanan;
				$data['bunga']=$r->bunga_pinjaman;
			}
		echo json_encode($data);	 
	 }
	 
	 function getdetailsimpanan()
	 {
		$id=$_POST['id_member'];
		$fld=empty($_POST['fld'])?"saldo_sukarela":$_POST['fld'];
		$this->main_model->table_name('anggota_v_sk');
		$this->main_model->query();
		$this->main_model->where(array('id_reg'=>$id));
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		echo ($lst_usr)?json_encode($lst_usr[0]):json_encode(array('simpanan_pokok'=>'0'));
	 }
	 function setpenarikan()
	 {
		$data=array();
		$tgl=explode("/",empty($_POST['tgl_trans'])?date('d/m/Y'):($_POST['tgl_trans']));
		$data['id_unit']	=empty($_POST['id_unit'])?'1':$_POST['id_unit'];
		$data['id_trans']   =empty($_POST['id_trans'])?'0':$_POST['id_trans'];
		$data['tgl_trans']  =empty($_POST['tgl_trans'])?date('Ymd'):tgltoSql($_POST['tgl_trans']);
		$data['jenis_trans']=empty($_POST['tp_setoran'])?'Pengambilan':$_POST['tp_setoran'];
		$data['bln']		=$tgl[1];
		$data['tahun']	  =$tgl[2];
		$data['id_member']  =empty($_POST['no_member'])?'':$_POST['no_member'];
		$data['id_simpanan']=empty($_POST['tp_simpanan'])?'3':$_POST['tp_simpanan'];
		$data['kredit']	 =0;
		$data['debet']      =empty($_POST['jml_simpanan'])?'0':str_replace(",","",$_POST['jml_simpanan']);
		$data['id_akun']    =empty($_POST['kd_akun'])?'':$_POST['kd_akun'];
		$data['id_akun_kontra']=empty($_POST['kd_akun2'])?'':$_POST['kd_akun2'];
		$data['ket_trans']  =empty($_POST['ket_trans'])?'':$_POST['ket_trans'];
		$data['created_by'] =$this->session->userdata("userid");
		$this->main_model->prepared_data('transaksi_member',$data);
		echo success($this->main_model->set_data());
	 }
	 function settariktotal()
	 {
		$data=array();$result="";
		$this->main_model->table_name('simpanan_jenis');
		$this->main_model->query();
		$this->main_model->where(array('id_calc'=>'1'));
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		foreach($lst_usr as $r)
		{
			$tgl=explode("/",empty($_POST['tgl_trans'])?date('d/m/Y'):($_POST['tgl_trans']));
			$data['id_unit']	=1;
			$data['id_trans']   =empty($_POST['id_trans'])?'0':$_POST['id_trans'];
			$data['tgl_trans']  =empty($_POST['tgl_trans'])?date('Ymd'):tgltoSql($_POST['tgl_trans']);
			$data['jenis_trans']=empty($_POST['tp_setoran'])?'Pengambilan':$_POST['tp_setoran'];
			$data['bln']		=$tgl[1];
			$data['tahun']	  =$tgl[2];
			$data['id_member']  =empty($_POST['no_member'])?$_POST['no_member2']:$_POST['no_member'];
			$data['id_simpanan']=$r->id_simpanan;
			$data['kredit']	 =0;
			switch($r->id_simpanan)
			{
				case '1':
					$data['debet']=empty($_POST['sp_pokok'])?'0':str_replace(",","",$_POST['sp_pokok']);
					$data['id_akun']=empty($_POST['sp_pokok'])?'':
					$r->id_akun.": ".$this->main_model->get_db('akun',array('ckode'=>$r->id_akun),'cnama');
				break;
				case '2':
					$data['debet']=empty($_POST['sp_wajib'])?'0':str_replace(",","",$_POST['sp_wajib']); 
					$data['id_akun']=empty($_POST['sp_wajib'])?'':
					$r->id_akun.": ".$this->main_model->get_db('akun',array('ckode'=>$r->id_akun),'cnama');
				break;
				case '3':
					$data['debet']=empty($_POST['sp_sukarela'])?'0':str_replace(",","",$_POST['sp_sukarela']); 
					$data['id_akun']=empty($_POST['sp_sukarela'])?'':
					$r->id_akun.": ".$this->main_model->get_db('akun',array('ckode'=>$r->id_akun),'cnama');
				break;
			}
			
			if($_POST['cara_bayar2']=='Tunai'){
				$data['id_akun_kontra']='10003: Kas Simpan Pinjam';
			}else{
				$data['id_akun_kontra']=empty($_POST['id_akun2'])?'':
					$_POST['id_akun2'].": ".$this->main_model->get_db('akun',array('ckode'=>$_POST['id_akun2']),'cnama');
			}
			$data['ket_trans']  =empty($_POST['ket_trans2'])?'':$_POST['ket_trans2'].'-'.$r->nm_simpanan;
			$data['created_by'] =$this->session->userdata("userid");
			$this->main_model->prepared_data('transaksi_member',$data);
			$result= success($this->main_model->set_data());
		}
		 echo $result;
	 }
	 //sesi pinjaman
	 function pinjam()
	 {
		$data['tab_header']=$this->zetro_form->generate_tab_header(($_GET['ot']=='y')?'Pinjaman Barang':'Pinjaman Tunai');
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_auth->menus(($_GET['ot']=='y')?'list_pinjaman_barang':'list_pinjaman_tunai');
		$this->zetro_form->section('frmlistpinjaman');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,false,true);
			//create list
			$content='';
			$this->zetro_form->section('listpinjaman');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std();
			//generate form add pinjaman
			$this->zetro_auth->menus('add_peminjam');
			$this->zetro_form->addButtons();
			if($_GET['ot']=='y'){
			$this->zetro_form->section('frmpinjamanbarang','','');
			//$this->zetro_form->frm_gabungan();
			}else{
			$this->zetro_form->section('frmpinjaman','','');
			}
			$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?false:false,'Simpan');
			$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm4','80%',true,'frm'),'Add Pinjaman','',true);
			$data['modal2']=$this->zetro_form->dialog('','Approved Pinjaman','2',true,'',false,'2');
			$data['lok'] =$this->zetro_form->generate_dropdown_content('simpanan_jenis','id_simpanan','nm_simpanan',($_GET['ot']=='y')?"id_simpanan in('4','6')":"id_simpanan in('5')");
			$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
			$data['bln']=dropdownBln(date('m'));
			$data['thn']=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan','',date('Y'));
		
			//load view
		$this->View($data,"pinjaman");	 
	 }
	 
	 function setpinjamanmember()
	 {
		 $data=array();
		 $data['id']=empty($_POST['id'])?'0':$_POST['id'];
		 $data['tgl_trans']=empty($_POST['tgl_trans'])?'':tgltoSql($_POST['tgl_trans']);
		 $data['id_member']=empty($_POST['no_member'])?'':$_POST['no_member'];
		 $data['jenis_pinjaman']=empty($_POST['jenis_pinjaman'])?'0':$_POST['jenis_pinjaman'];
		 $data['jml_pinjaman']=empty($_POST['jml_pinjaman'])?'0':str_replace(",","",$_POST['jml_pinjaman']);
		 $data['lama_cicilan']=empty($_POST['cicilan'])?'0':str_replace(",","",$_POST['cicilan']);
		 $data['jml_cicilan']=empty($_POST['jml_cicilan'])?'0':str_replace(",","",$_POST['jml_cicilan']);
		 $data['jml_cicilan_bunga']=empty($_POST['jml_cicilan_bunga'])?'0':str_replace(",","",$_POST['jml_cicilan_bunga']);
		 $data['saldo_pinjaman']=empty($_POST['saldo_pinjaman'])?'0':str_replace(",","",$_POST['saldo_pinjaman']);
		 $data['cara_bayar']=empty($_POST['cara_bayar'])?'Tunai':$_POST['cara_bayar'];
		 $data['id_akun']=empty($_POST['id_akun'])?'':$_POST['id_akun'];
		 $data['id_akun_kontra']=empty($_POST['id_akun2'])?'':$_POST['id_akun2'];
		 $data['ket_trans']=empty($_POST['ket_pinjaman'])?'':$_POST['ket_pinjaman'];
		 $data['bunga_pinjaman']=empty($_POST['bunga_pinjaman'])?'0':str_replace('%','',$_POST['bunga_pinjaman']);
		 $data['created_by']=$this->session->userdata("userid");
			$this->main_model->prepared_data('transaksi_pinjaman',$data);
			echo ($this->main_model->set_data())?$this->main_model->rdb("transaksi_pinjaman","doc_no","where id=LAST_INSERT_ID()"):"";
		 
	 }
	 function setdetailpinjaman()
	 {
		$data=array();
		$data['doc_no']=empty($_POST['doc_no'])?'':$_PODT['doc_no'];
		$data['id_barang']=empty($_POST['id_barang'])?'':$_PODT['id_barang'];
		$data['jml_barang']=empty($_POST['jml_barang'])?'0':$_PODT['jml_barang'];
		$data['harga_barang']=empty($_POST['harga_barang'])?'0':str_replace(",","",$_PODT['harga_barang']);
		$data['id_unit']=empty($_POST['id_unit'])?'':$_PODT['id_unit'];
		$this->main_model->prepared_data('transaksi_member',$data);
		echo success($this->main_model->set_data());
	 
	 }
	 
	 function getlistpinjaman()
	 {
		 $lst_usr=array();$n=0;$content='';
		 $bln=empty($_POST['bln'])?date('m'):$_POST['bln'];
		 $thn=empty($_POST['thn'])?date('Y'):$_POST['thn'];
		$this->main_model->table_name('transaksi_pinjaman_v');
		$this->main_model->query();
		($_POST['ot']=='p')?'':
		$this->main_model->where(array('jenis_pinjaman'=>($_POST['ot']=='y')?'4':'5'));
		empty($_POST['id_lokasi'])?'':
		$this->main_model->where(array('id_lokasi'=>$_POST['id_lokasi']));
		($_POST['ot']!='p')?'':
		$this->main_model->where_in('stat_pinjaman',array('N'));//,'Y','D','C'));
		$this->main_model->where(array('month(tgl_trans)'=>$bln,'year(tgl_trans)'=>$thn));
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		$ot=empty($_POST['ot'])?'':$_POST['ot'];
		switch($ot)
		{
			case 'y':
				$this->zetro_auth->menus('list_pinjaman_barang');
				$this->zetro_func->menus('pinjaman_barang');
			break;
			case 'n':
				$this->zetro_auth->menus('list_pinjaman_tunai');
				$this->zetro_func->menus('pinjaman_tunai');
			break;
			case 'p':
				$this->zetro_auth->menus('realisasi_pinjaman');
				$this->zetro_func->menus('realisasi');
			break;
		}
		
		foreach($lst_usr as $r)
		{
			$n++;$oto="";$icon="";
			$oto=$this->zetro_auth->cek_oto('e');
			$this->zetro_func->table_name('transaksi_pinjaman_app');
			$this->zetro_func->field_name('app_level');
			$this->zetro_func->wherex(array('id_member'=>$r->id_member));
			$icon=($oto!='')?
			$this->zetro_form->z_icon($r->id,array(($ot!='p')?'cus-':$this->zetro_func->set_approvals('app_stat'),($ot=='p')?'cus-':'cus-folder','cus-cross'),
											 array(($ot!='p')?'':$this->zetro_func->set_approvals('app_stat','xx'),($ot=='p')?'':'update','deleted')):"";
			$vicon=$this->zetro_form->z_icon($r->id,array('cus-zoom','cus-','cus-'),array('show','',''));
			$content.=tr().td($n,'',false,'','center').
					  td($r->id_member,'',false,'','center').
					  td($r->nm_member).
					  td(number_format($r->jml_pinjaman,2),'',false,'','right').
					  td($r->lama_cicilan,'',false,'','center').
					  td(number_format($r->jml_cicilan,2),'',false,'','right').
					  td(number_format($r->jml_cicilan_bunga,2),'',false,'','right').
					  td(number_format(($r->jml_cicilan+$r->jml_cicilan_bunga),2),'',false,'','right').
					  td($r->cbayare,'',false,'','center').
					  td(($ot!='p')?$r->ket_trans:$r->nm_simpanan).
					  td($r->stat_pinjaman,'',false,'','center').
					  td(($r->stat_pinjaman!='N')?$vicon:$icon,'',false,'','center').
					 _tr();
		}
		$content.=$this->zetro_form->DataNotFound($lst_usr,10);
		echo $content;
	 }
	 
	 function getdetailpinjaman()
	 {
		$data=array();
		$id=$_POST['id'];
		$this->main_model->table_name('transaksi_pinjaman_v');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id'=>$id));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);
		 
	 }
	 
	 function deletepinbar()
	 {
		$id=$_POST['id'];
		$this->main_model->table_name('transaksi_pinjaman');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id'=>$id));
		echo success($this->main_model->set_data());	
		 
	 }
	 
	 //realisasi
	 function realisasi()
	 {
		$data['tab_header']=$this->zetro_form->generate_tab_header('Approval');
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_auth->menus('approval_pinjaman');
		$this->zetro_form->section('frmlistpinjaman');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,false,true);
			//create list
			$content='';
			$this->zetro_form->section('listpinjaman');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std();
			//generate form add pinjaman
			$this->zetro_auth->menus('add_peminjam');
			$this->zetro_form->addButtons();
			$this->zetro_form->section('frmpinjaman','','');
			$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?false:false,'Simpan');
			$this->zetro_form->frm_std('frm4','80%',true,'frm');
			$data['modal']=$this->zetro_form->dialog('','Approved Pinjaman');
			$data['lok'] =$this->zetro_form->generate_dropdown_content('simpanan_jenis','id_simpanan','nm_simpanan',"id_simpanan in('4','5')");
			$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
			$data['bln']=dropdownBln(date('m'));
			$data['thn']=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan','',date('Y'));
		
			//load view
		$this->View($data,"realisasi");	 
	 }
	 function realisasi_approve()
	 {
		$data=array();$content="";
		$id=$_POST['id'];
		$this->main_model->table_name('transaksi_pinjaman_v');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id'=>$id));
		$app_stat=$this->main_model->rdb('transaksi_pinjaman_v','stat_pinjaman',"where id='".$id."'");
		$data=$this->main_model->set_data();
		//print_r($data);
		foreach($data as $r)
		{
			$saldo=($r->jenis_pinjaman=='4')?($r->saldo_pinjaman_barang):($r->saldo_pinjaman_uang);
		echo tabel().
				  tr().td('No. Anggota','40%',true,'','left').td($r->id_member,'',true,'','left')._tr().
				  tr().td('Naman. Anggota').td($r->nm_member)._tr().
				  tr().td('Jenis Pinjaman').td($r->nm_simpanan)._tr().
				  tr().td('Saldo Pinjaman').td(number_format($saldo,2))._tr().
				  tr().td('Limit Pinjaman').td(number_format(($r->max_limit-$saldo),2))._tr().
				  tr().td('Jumlah Pinjaman').td(number_format($r->jml_pinjaman,2))._tr().
				  tr().td('Lama Cicilan').td(number_format($r->lama_cicilan,2)." Bulan")._tr().
				  tr().td('','',false,'alert-info','center',"colspan='2'")._tr();
		}
		$this->main_model->table_name('transaksi_pinjaman_app');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id_trans'=>$id));
		$app_stat=$this->main_model->rdb('transaksi_pinjaman_v','stat_pinjaman',"where id='".$id."'");
		$lst_usr=$this->main_model->set_data();
				  $n=0;
		foreach($lst_usr as $r)
		{
					if($r->app_level >0)
					{
						$n++;
						echo tr().td('Approved '.$n.' by').td($r->app_by)._tr().
							 tr().td('Approval Date').td(tglFromSql($r->app_date,true))._tr();
					}
		}
					$datax="<button class='btn btn-small btn-primary' id='approved' onClick=\"btn_click('".$id.":".($n+1)."','appr')\"><i class='cus-cog'></i> Approved</button>\n";
					$datax.="<button class='btn btn-small' id='cancel' onClick=\"btn_click('".$id.":".($n+1)."','noappr')\"><i class='cus-cog'></i> Un Approved</button>\n";
					$approper=$this->main_model->rdb('v_app_doc','app_level',"where id_doc='realisasi'");
					$n_app=(($n+1)<= $approper)?$this->main_model->rdb('v_app_doc','level_'.($n+1),"where id_doc='realisasi'"):'';
					$det=($app_stat=='D')?
						 "<mark>".$this->main_model->rdb('transaksi_pinjaman_app','app_reason',"where id_trans='".$id."'")."</mark><br> on ".
						 tglFromSql($this->main_model->rdb('transaksi_pinjaman_app','app_date',"where id_trans='".$id."'"),true):
						 "<mark>Wait for approval</mark>";
					if($n < $approper)
					{
						echo tr().td(($app_stat=='D')?'Not Approved with reason':'Approved '.($n+1)).
							 td(($this->session->userdata('userid')==$n_app && $app_stat!='D' )?$datax:$det)._tr().
							 tr().td(($app_stat=='D')?'Deleted by':'Approval must do by').
							 td($this->main_model->rdb('v_users','username',"where userid='".$n_app."'"))._tr();
						
					}
			echo _tabel();
		
			 
	 }
	 function update_approval()
	 {
		$id	 =$_POST['id'];
		$lvl	=$_POST['lvl'];
		$rsn	=empty($_POST['rsn'])?'':$_POST['rsn'];
		$approper=$this->main_model->rdb('v_app_doc','app_level',"where id_doc='realisasi'");
		$this->main_model->table_name('transaksi_pinjaman');
		$this->main_model->set_methods('update');
		($lvl==$approper)?
		$this->main_model->get_content(array('app_level'=>$lvl,'stat_pinjaman'=>'Y','created_by'=>$this->session->userdata('userid'))):
		$this->main_model->get_content(array('app_level'=>$lvl,'stat_pinjaman'=>'N','created_by'=>$this->session->userdata('userid')));
		$this->main_model->where(array('id'=>$id));
		$this->main_model->set_data();	
	 }
	function deleted_register()
	{
		//merubah status member menjadi C (close) masukan alasan tidak di setujui
		$id	 =$_POST['id'];
		$lvl	=empty($_POST['lvl'])?$this->main_model->rdb('transaksi_pinjaman_app','app_level',"where id_reg='".$id."'"):$_POST['lvl'];
		$rsn	=empty($_POST['rsn'])?'':$_POST['rsn'];
		//update table member
		$this->main_model->table_name('transaksi_pinjaman');
		$this->main_model->set_methods('update');
		$this->main_model->get_content(array('stat_pinjaman'=>'D','created_by'=>$this->session->userdata('userid')));
		$this->main_model->where(array('id'=>$id));
		$this->main_model->set_data();	
		//update reason for table member_reg
		$this->main_model->table_name('transaksi_pinjaman_app');
		$this->main_model->set_methods('update');
		$this->main_model->get_content(array('app_level'=>$lvl,'app_reason'=>$rsn));
		$this->main_model->where(array('id_trans'=>$id,'app_level'=>($lvl-1)));
		$this->main_model->set_data();	
	}
	//limit pinjaman
	
	function limitpinjaman()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Limit Pinjaman');
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_auth->menus('limit_pinjaman');
		$content='';
			$this->zetro_form->section('limitpinjaman');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std('NewList','60%');
			//generate form add pinjaman
			$this->zetro_auth->menus('limit_pinjaman');
			$this->zetro_form->addButtons();
			$this->zetro_form->section('frmlimitpinjaman','','');
			$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?false:false,'Simpan');
			$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm4','80%',true,'frm'),'Limit Pinjaman','',true);
		
		//load view
		$this->View($data,"limit");	 
	}
	
	function setlimitpinjaman()
	{
		$data=array();
		//$data['id_simpanan']=empty($_POST['id_simpanan'])?'0':$_POST['id_simpanan'];
		//$data['id_simpanan']=empty($_POST['nm_simpanan'])?'':$_POST['nm_simpanan'];
		$data['max_limit']=empty($_POST['max_limit'])?'0':str_replace(",","",$_POST['max_limit']);
		$data['created_by']=$this->session->userdata['userid'];
		$this->main_model->where(array('id_simpanan'=>$_POST['id_simpanan']));
		$this->main_model->prepared_data('simpanan_jenis',$data,'update');
		echo success($this->main_model->set_data());
	}
	
	function getlimitpinjaman()
	{
		$lst_usr=array();$result="";$n=0;
		$this->main_model->table_name('simpanan_jenis');
		$this->main_model->query();
		$this->main_model->where(array('id_calc'=>'2'));
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		foreach($lst_usr as $r)
		{
			$n++;
			$this->zetro_auth->menus('limit_pinjaman');
			$icon=($this->zetro_auth->cek_oto('e')!='')?
					  $this->zetro_form->z_icon($r->id_simpanan,
					  		array('cus-folder','cus-delete'),
							array('edit','delete')):'';
			$result.=tr().td($n,'',false,'','center').
					 td($r->nm_simpanan).
					 td(number_format($r->max_limit,2),'',false,'','right').
					 td($icon,'',false,'','center').
					 _tr();
		}
		$result.=$this->zetro_form->DataNotFound($lst_usr,4);
		echo $result;
	}
	
	function getdetaillimit()
	{
		$data=array();
		$id=$_POST['id'];
		$this->main_model->table_name('simpanan_jenis');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id_simpanan'=>$id));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	}
	
	function deletelimitpinjaman()
	{
	}
	//pencairan
	function pencairan()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Realisasi');
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_auth->menus('list_pinjaman_approved');
		$this->zetro_form->section('frmlistpinjaman');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,false,true);
		$this->zetro_form->tp_ahead($this->get_kdcc());
			//create list
			$content='';
			$this->zetro_form->section('loanlist');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std();
			//generate form add pinjaman
			//$this->zetro_auth->menus('add_peminjam');
			$this->zetro_form->addButtons();
			$this->zetro_form->section('loanreal','','');
			$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?false:false,'Simpan');
			$this->zetro_form->frm_std('frm4','80%',true,'frm');
			$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm4','80%',true,'frm'),'Pencairan Pinjaman','',($this->zetro_auth->cek_oto('c')!='')?true:false);
			$this->zetro_form->addButtons();
			$this->zetro_form->section('biaya2','','');
			$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?false:false,'Simpan');
			$this->zetro_form->frm_std('frm5','80%',true,'frm');
			$data['modal2']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm5','80%',true,'frm'),'Pencairan Pinjaman','2',($this->zetro_auth->cek_oto('c')!='')?true:false,'',false,'2');
			$data['lok'] =$this->zetro_form->generate_dropdown_content('simpanan_jenis','id_simpanan','nm_simpanan',"id_simpanan >=4");
			$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
			$data['bln']=dropdownBln(date('m'));
			$data['thn']=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan','',date('Y'));
			$data['modal3']=$this->zetro_form->PrintPreview('Print Bukti Pencairan');
		
			//load view
		$this->View($data,"pencairan");	 
		
		
	}
	
	function setpencairan()
	{
		$data=array();
		$data['real_pinjaman']=empty($_POST['real_pinjaman'])?'0':str_replace(',','',$_POST['real_pinjaman']);
		$data['jml_cicilan']=str_replace(',','',$_POST['jml_cicilan']);
		$data['jml_cicilan_bunga']=str_replace(',','',$_POST['jml_cicilan_bunga']);
		$data['lama_cicilan']=str_replace(',','',$_POST['lama_cicilan']);
		$data['bunga_pinjaman']=str_replace(',','',$_POST['bunga_pinjaman']);
		$data['ket_trans']=empty($_POST['ket_pinjaman'])?'':$_POST['ket_pinjaman'];
		$data['tgl_real']=tglToSql($_POST['tgl_real']);
		$data['stat_pinjaman']='C';
		$data['created_by']=$this->session->userdata['userid'];
		$data['jenis_trans']=empty($_POST['id_cc'])?'':$_POST['id_cc'];
		$this->main_model->where(array('id'=>$_POST['id_pinjaman']));
		$this->main_model->prepared_data('transaksi_pinjaman',$data,'update');
		echo ($this->main_model->set_data())?0:1;
		
	}
	function settransaksipinjaman($id_cc='')
	{
		$id_cc=explode(":",$_POST['kd_cc']);
		$data=array();$datax=array();$n=0;
		$this->main_model->table_name('akun_cc_view');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('parent_cc'=>$id_cc[2]));
		$datax=$this->main_model->set_data();
		$tgl=explode("/",$_POST['tgl_real']);
		foreach($datax as $r)
		{
			
			$kredit=0;
			$nm_member=empty($_POST['nm_member'])?'':$_POST['nm_member'];
			$kredit=empty($_POST[$r->fld_name])?'0':str_replace(",","",$_POST[$r->fld_name]);
			$data['id_trans']   =0;
			$data['tgl_trans']  =empty($_POST['tgl_real'])?date('Ymd'):tgltoSql($_POST['tgl_real']);
			$data['jenis_trans']=empty($_POST['tp_setoran'])?'Pinjaman':$_POST['tp_setoran'];
			$data['bln']		=$tgl[1];
			$data['tahun']	  =$tgl[2];
			$data['id_unit']	='1';
			$data['id_member']  =empty($_POST['no_member'])?'':$_POST['no_member'];
			$data['id_simpanan']=empty($_POST['jenis_pinjaman'])?'':$_POST['jenis_pinjaman'];
			$data['kredit']     =($r->id_calc==1)?$kredit:0;
			$data['debet']	  =($r->id_calc==0)?$kredit:0;
			$data['id_akun']    =($r->id_calc==1)?$r->kd_akun_kontra:'';
			$data['id_akun_kontra']=($r->id_calc==1)?$r->kd_akun:$r->kd_akun;
			$data['ket_trans']  =$r->id_akun." - ".$r->nm_cc." - ". $nm_member;
			$data['stat_trans'] =empty($_POST['stat_trans'])?'N':$_POST['stat_trans'];
			$data['created_by'] =$this->session->userdata("userid");
			$this->main_model->prepared_data('transaksi_member',$data);
			echo success($this->main_model->set_data());
		}
	}
	function set_pelunasan()
	{
		$id_cc=explode(":",empty($_POST['cc'])?'::46':$_POST['cc']);
		$data=array();$datax=array();$n=0;
		$this->main_model->table_name('akun_cc_view');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('parent_cc'=>$id_cc[2]));
		$datax=$this->main_model->set_data();
		$tgl=explode("/",$_POST['tgl_real']);
		foreach($datax as $r)
		{
			
			$kredit=0;
			$nm_member=empty($_POST['nm_member'])?'':$_POST['nm_member'];
			$kredit=empty($_POST[$r->fld_name])?'0':str_replace(",","",$_POST[$r->fld_name]);
			$data['id_trans']   =0;
			$data['tgl_trans']  =empty($_POST['tgl_real'])?date('Ymd'):tgltoSql($_POST['tgl_real']);
			$data['jenis_trans']=empty($_POST['tp_setoran'])?'Pinjaman':$_POST['tp_setoran'];
			$data['bln']		=$tgl[1];
			$data['tahun']	  =$tgl[2];
			$data['id_unit']	='1';
			$data['id_member']  =empty($_POST['no_member'])?'':$_POST['no_member'];
			$data['id_simpanan']=empty($_POST['jenis_pinjaman'])?'5':$_POST['jenis_pinjaman'];
			$data['kredit']     =($r->id_calc==1)?$kredit:0;
			$data['debet']	  =($r->id_calc==0)?$kredit:0;
			$data['id_akun']    =($r->id_calc==1)?$r->kd_akun_kontra:'';
			$data['id_akun_kontra']=($r->id_calc==1)?$r->kd_akun:$r->kd_akun;
			$data['ket_trans']  ="Pelunasan Pinjaman - ". $nm_member;
			$data['stat_trans'] =empty($_POST['stat_trans'])?'N':$_POST['stat_trans'];
			$data['created_by'] =$this->session->userdata("userid");
			$this->main_model->prepared_data('transaksi_member',$data);
			echo success($this->main_model->set_data());
		}
	}
	function setpencairanbiaya()
	{
		$data=array();
		$data['id_trans']=empty($_POST['id_pinjaman2'])?'0':($_POST['id_pinjaman2']);
		$data['jml_pinjaman']=empty($_POST['jml_pinjaman2'])?'0':str_replace(',','',$_POST['jml_pinjaman2']);
		$data['provisi']=empty($_POST['provisi'])?'0':str_replace(',','',$_POST['provisi']);
		$data['materai']=empty($_POST['materai'])?'0':str_replace(',','',$_POST['materai']);
		$data['lain_lain']=empty($_POST['lain_lain'])?'0':str_replace(',','',$_POST['lain_lain']);
		$data['total_terima']=empty($_POST['diterima'])?'0':str_replace(',','',$_POST['diterima']);
		$data['keterangan']=empty($_POST['ket_pinjaman2'])?'':$_POST['ket_pinjaman2'];
		$data['id_akun']=$_POST['kd_akun12'];
		$data['id_akun_kontra']=$_POST['kd_akun22'];
		$data['created_by']=$this->session->userdata['userid'];
		$this->main_model->prepared_data('transaksi_pinjaman_biaya',$data);
		echo success($this->main_model->set_data());
		
	}
	function printbuktipencairan()
	{
		$data=array();
		$data['coy']	=$this->zetro_form->infoCo('Nama');
		$data['add']	=$this->zetro_form->infoCo('Address');
		$data['kota']   =$this->zetro_form->infoCo('City');
		$data['keua']   =$this->zetro_form->infoCo('AccHead');
		$id=$_POST['id'];
		$this->main_model->where(array('id'=>$id));
		$this->main_model->table_name('transaksi_pinjaman_v');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data['lst']=$this->main_model->set_data();
		//print_r($data);
		$this->load->library('zetro_dompdf');
		$html = $this->load->view('simpanpinjam/realisasi_print', $data, true);
		$this->zetro_dompdf->createPDF($html, $this->session->userdata('userid').'_realisasi_print');
		echo base_url().'application/logs/'.$this->session->userdata('userid').'_realisasi_print.pdf';
	}
	function getlistpencairan()
	{
		$lst_usr=array();$n=0;$content='';
		$bln=empty($_POST['bln'])?date('m'):$_POST['bln'];
		$thn=empty($_POST['thn'])?date('Y'):$_POST['thn'];
		$this->main_model->table_name('transaksi_pinjaman_v');
		$this->main_model->query();
		$this->main_model->where(array('stat_pinjaman'=>'Y'));
		empty($_POST['id_lokasi'])?'':
		$this->main_model->where(array('id_lokasi'=>$_POST['id_lokasi']));
		$this->main_model->where(array('month(tgl_trans)'=>$bln,'year(tgl_trans)'=>$thn));
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		$this->zetro_auth->menus('list_pinjaman_approved');
		foreach($lst_usr as $r)
		{
			$n++;
			$icon=($this->zetro_auth->cek_oto('c')!='')?
					  $this->zetro_form->z_icon($r->id,
					  		array('cus-folder-database'),
							array(($r->jenis_pinjaman!=5)?'biaya':'pencairan')):'';
			$content.=tr().
					 td(tglFromSql($r->tgl_trans),'',false,'','center').
					 td($r->nm_member).
					 td(number_format($r->jml_pinjaman,2),'',false,'','right').
					 td(number_format($r->real_pinjaman,2),'',false,'','right').
					 td(number_format(((int)$r->provisi+(int)$r->materai+(int)$r->lain_lain),2),'',false,'','right').
					 td(number_format(($r->jenis_pinjaman==5)?$r->total_terima:$r->real_pinjaman,2),'',false,'','right').
					 td($r->lama_cicilan,'',false,'','center').
					 td(number_format($r->jml_cicilan,2),'',false,'','right').
					 td(number_format($r->jml_cicilan_bunga,2),'',false,'','right').
					 td($r->ket_trans).
					 td($icon,'',false,'','center').
					 _tr();
		}
		$content.=$this->zetro_form->DataNotFound($lst_usr,10);
		echo $content;
	}
	
	function getdetailpencairan()
	{
		$data=array();
		$id=$_POST['id'];
		$this->main_model->table_name('transaksi_pinjaman_v');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id_simpanan'=>$id));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	
	}
	
	function delpencairan()
	{
	}
	
	//pin book
	function pinbook()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Pin Book');
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_auth->menus('pin_book');
		$this->zetro_form->buttons(false,'Print');
		$this->zetro_form->section('frmpinbook');
		$data['frm3']=$this->zetro_form->frmInline('frm1',false,($this->zetro_auth->cek_oto('v')!='')?true:false,true);
		$data['lok']=$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
		$this->zetro_form->tp_ahead($this->get_akun());
			//create list
			$content='';
			$this->zetro_form->section('pinbook');
			$this->zetro_form->action_col(false);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std();
			$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
			$data['bln']=dropdownBln(date('m'));
			$data['thn']=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan','',date('Y'));
		//load view
		$this->View($data,"pinbook");
		
	}
	
	function getpinbook()
	{
		$content="";$n=0;
		$id_lok=empty($_POST['id_cabang'])?'':$_POST['id_cabang'];
		$thn=$_POST['tahun'];
		$bln=$_POST['bln'];
		$lst_usr=array();
			$ordby=empty($_POST['ordby'])?'nm_member':$_POST['ordby'];
			$this->main_model->table_name('v_member_sp');
			$this->main_model->query();
			(empty($_POST['id_cabang'])||$_POST['id_cabang']=='')?'':
			$this->main_model->where(array('id_lokasi'=>$id_lok,'c_bayar'=>'2'));
			$this->main_model->where(array('status_member <>'=>'N'));
			(empty($_POST['car']))?'':
			$this->main_model->where_like('nm_member',$_POST['cari'],'after');
			$this->main_model->group_by('id_reg');
			$this->main_model->orderby($ordby);
			$this->main_model->set_methods('select_ordered');
			$lst_usr=$this->main_model->set_data();
			$this->zetro_auth->menus('pin_book');
			foreach($lst_usr as $r)
			{
				 
				$n++;
				$oto=$this->zetro_auth->cek_oto('c');$posting='';$ck="";$check="";
				$content.=tr().td($n,false,'','','center').
						  td($r->nip_member,false,'','','left').
						  td($r->nm_member).
						  td($r->rekening).
						  td(number_format($this->_get_total_simpanan($r->id_reg,'2'),0),false,'','','right').
						  td('0013200103641','',false,'','center').
						  td('Pot. Kopkar Bulan '. nBulan($bln)." ".$thn).
						  _tr();
				
			}
		$content.=$this->zetro_form->DataNotFound($lst_usr,7);
		echo $content;
	}
	
	function _get_total_simpanan($id_member)
	{
		$val=0;$lst_usr=array();
		$this->main_model->table_name('member_set_simpanan');
		$this->main_model->query();
		$this->main_model->where(array('id_member'=>$id_member));
			$this->main_model->set_methods('select');
			$lst_usr=$this->main_model->set_data();
			foreach($lst_usr as $rr)
			{
				$val+=$rr->val_simpanan;
			}
		return $val;
	}
}
